import pymysql
from gis空间包含统计.SqlConfig.sqls import get_event_any_type_sql
import glob
import os.path
import shutil


def helper(image_dir):
    connection = pymysql.connect(
        host="localhost",
        database="cgdb",
        user="root",
        password="egova",
        port=3306
    )
    cursor = connection.cursor()
    cursor.execute(get_event_any_type_sql)
    result = cursor.fetchall()
    subTypeNameDic = {}
    target_dir = os.path.join(image_dir, "target");
    for record in result:
        subTypeNameDic[record[1]] = record[2]
    if not os.path.exists(target_dir):
        os.makedirs(target_dir)
    print(len(subTypeNameDic))
    for image_path in glob.glob(image_dir + "/*.png"):
        base_name = os.path.basename(image_path)
        filename_without_ext, file_extension = os.path.splitext(base_name)
        if subTypeNameDic.get(filename_without_ext) is not None:
            # print(base_name, subTypeNameDic.get(filename_without_ext))
            new_name = subTypeNameDic.get(filename_without_ext) + ".png"
            new_path = os.path.join(target_dir, new_name)
            # print(new_path)
            shutil.copy(image_path, new_path)
        elif "@" not in filename_without_ext:
            print(image_path)


if __name__ == '__main__':
    helper(r"C:\Users\17673\Downloads\部件弹窗图标")
